package cn.edu.nwpu.warehousemanagement.controller;

import cn.edu.nwpu.warehousemanagement.pojo.InventoryConfig;
import cn.edu.nwpu.warehousemanagement.pojo.Result;
import cn.edu.nwpu.warehousemanagement.pojo.dto.IDDTO;
import cn.edu.nwpu.warehousemanagement.pojo.dto.PageDTO;
import cn.edu.nwpu.warehousemanagement.service.InventoryConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 库存配置控制器
 * @author LSR
 */
@RestController
@RequestMapping("/api/inventory-config")
@RequiredArgsConstructor
public class InventoryConfigController {

    private final InventoryConfigService inventoryConfigService;

    @PostMapping("/create")
    public Result<InventoryConfig> create(@RequestBody InventoryConfig config) {
        if (config.getProduct() == null || config.getProduct().getId() == null) {
            return Result.error("商品ID不能为空");
        }
        return inventoryConfigService.create(config);
    }

    @PostMapping("/delete-by-id")
    public Result<Void> delete(@RequestBody Long delete_id) {
        if (delete_id == null) {
            return Result.error("ID不能为空");
        }
        return inventoryConfigService.delete(delete_id);
    }

    @PostMapping("/delete-by-product-id")
    public Result<Void> delete(@RequestBody IDDTO delete_product_id) {
        if (delete_product_id.getId() == null) {
            return Result.error("ProductID不能为空");
        }
        return inventoryConfigService.deleteByProductId(delete_product_id.getId());
    }

    @PostMapping("/update")
    public Result<InventoryConfig> update(@RequestBody InventoryConfig config) {
        return inventoryConfigService.update(config);
    }

    @PostMapping("/get-by-id")
    public Result<InventoryConfig> getById(@RequestBody Long inventoryConfig_id) {
        if (inventoryConfig_id == null) {
            return Result.error("ID不能为空");
        }
        return inventoryConfigService.getById(inventoryConfig_id);
    }

    @PostMapping("/get-by-product-id")
    public Result<InventoryConfig> getByProductId(@RequestBody Long productId) {
        if (productId == null) {
            return Result.error("商品ID不能为空");
        }
        return inventoryConfigService.getByProductId(productId);
    }

    @PostMapping("/list-by-page")
    public Result<Page<InventoryConfig>> listByPage(@RequestBody PageDTO pageDTO) {
        if (pageDTO.getPageNum() < 1) {
            return Result.error("页码必须大于0");
        }
        return inventoryConfigService.listByPage(pageDTO);
    }

    @PostMapping("/get-all-inventory-config-pages")
    public Result<List<Page<InventoryConfig>>> getAllInventoryConfigPages() {
        return inventoryConfigService.getAllInventoryConfigPages();
    }

    @PostMapping("/get-all-inventory-config-list")
    public Result<List<InventoryConfig>> getAllInventoryConfigList() {
        return inventoryConfigService.getAllInventoryConfigList();
    }
}